Skip to content

Add ForagerBehavior example — GSoC 2026 behavioral framework prototype#392

Open
1khaled-ctrl wants to merge 14 commits intomesa:mainfrom
1khaled-ctrl:main
Open

Add ForagerBehavior example — GSoC 2026 behavioral framework prototype#392
1khaled-ctrl wants to merge 14 commits intomesa:mainfrom
1khaled-ctrl:main

Conversation

@1khaled-ctrl
Copy link

What this adds

A new example demonstrating a modular behavioral framework for Mesa agents.

ForagerBehavior model

Agents switch between behavioral states:

  • searching — moves randomly and forages for resources
  • resting — recovers energy when exhausted

Transition logic:

  • searching → resting when energy <= 20
  • resting → searching when energy >= 80

Files added

  • model.py — ForagerAgent and ForagerModel
  • app.py — SolaraViz visualization with sliders
  • README.md — explanation and usage guide

Context

This example is part of my GSoC 2026 proposal:
Behavioral Framework for Agent Models in Mesa

The full prototype is at:
https://github.com/1khaled-ctrl/mesa-behavior-framework-prototype

— Khaled Saber

@EwoutH
Copy link
Member

EwoutH commented Mar 17, 2026

Thanks! Can you do a self-review and then request a peer review, following:

Copy link
Author

@1khaled-ctrl 1khaled-ctrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review complete. Code is clean and tested locally.

@1khaled-ctrl 1khaled-ctrl marked this pull request as draft March 17, 2026 14:39
@1khaled-ctrl
Copy link
Author

Hi @EwoutH — self-review is complete. Code is clean and tested locally.
Could you please review when you have time? Thank you!

import mesa


class ForagerAgent(mesa.Agent):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to use appropriate agent class from discrete_space

resource_density=0.3,
):
super().__init__()
self.grid = mesa.space.MultiGrid(width, height, torus=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mesa.space has been removed for 4.0, Use mesa.discrete_space instead

for y in range(height):
if self.random.random() < resource_density:
self.resources[(x, y)] = 20.0
for _ in range(n_agents):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to use create_agents method instead

## How to Run
```bash
pip install mesa
python run_simulation.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find run_simulation.py

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find run_simulation.py

Good catch! I will switch to the appropriate agent class from discrete_space.

@Harshini2411
Copy link

Hey, cool concept! A few things to fix though:

  • Indentation bug in ForagerModel.init everything after super().init() is at class level instead of inside the method. This will crash on run.
  • mesa.space.MultiGrid is gone in Mesa 4, switch to mesa.discrete_space. This also affects _move() and _forage() since agents now use self.cell instead of self.pos.
  • run_simulation.py doesn't exist, README should say solara run app.py instead.
  • import random isn't needed, use self.random instead for reproducibility.
  • README describes StateMachine, BehaviorModules, etc. but the actual code is just if/else logic worth syncing those up.

Fix the crashes first and it'll be in much better shape!

Updated instructions to run the simulation with Solara.
Updated the 'How to Run' section formatting in README.
Added instructions for running the ForagerBehavior example.
@1khaled-ctrl 1khaled-ctrl marked this pull request as ready for review March 21, 2026 15:17
@1khaled-ctrl
Copy link
Author

Hi @EwoutH @codebreaker32 @Harshini2411 — I have addressed all the feedback:

  • Switched from mesa.space.MultiGrid to mesa.discrete_space
  • Updated self.pos to self.cell
  • Removed import random, using self.random instead
  • Fixed README to say solara run app.py
  • Fixed indentation in ForagerModel.init

Ready for re-review. Thank you!

@Harshini2411
Copy link

The Mesa 4 fixes look good! One thing still outstanding though the README still mentions StateMachine, BehaviorModules, EnergyDepletionBehavior etc. which don't exist in the code. Just update it to describe what the model actually does and it's ready!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants